package com.obixlabs.security.credentialsstore.model;

import javax.xml.bind.annotation.XmlElement;

import com.obixlabs.commons.types.ObixSimpleType;

public class ObixGroup extends ObixSimpleType
{
	private static final long serialVersionUID = 3060329732841741384L;
	
	private GroupPermissions permissions;
	
	public ObixGroup() 
	{ setPermissions(new GroupPermissions());}		
	
	@XmlElement
	public GroupPermissions getPermissions() 
	{return permissions;}
	public void setPermissions(GroupPermissions permissions) 
	{this.permissions = permissions;}

	public boolean assignPermission(ObixPermission permission)
	{return this.permissions.assignPermission(permission);}
	
	public boolean revokePermission(ObixPermission permission)
	{return this.permissions.revokePermission(permission);}

	public boolean assignAllPermissions(ObixPermissions newPermissions) 
	{
		boolean result = true;
		boolean addResult;
		for (ObixPermission perm : newPermissions.values())
		{
			addResult = assignPermission(perm);
			if (result) result = addResult;
		}
		return result;
	}

	@Override
	public boolean equals(Object obj) 
	{
		if (this == obj) return true;
		
		if (!(obj instanceof ObixGroup)) return false;
		
		ObixGroup other = (ObixGroup) obj;
		if (permissions == null) 
		{
			if (other.permissions != null)
				return false;
		} 
		else if (!permissions.equals(other.permissions))
			return false;
		
		return true;
	}
	
}